package com.jic.market.controller.manage;

import com.alibaba.fastjson.JSON;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.market.api.manage.PromotionManageApi;
import com.jic.market.enums.PromotionStatusEnum;
import com.jic.market.request.MemberGradeInfoRequest;
import com.jic.market.request.PromotionMemberGradeRequest;
import com.jic.market.request.PromotionRequest;
import com.jic.market.request.QueryPromotionListRequest;
import com.jic.market.response.PageResponse;
import com.jic.market.response.PromotionResponse;
import com.jic.market.service.JicMarketPromotionService;
import com.jic.market.service.PromotionMemberGradeService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


/**
 * description: PromotionManageController
 * date: 2020-01-10 11:58
 * author: wangys
 * version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("promotion")
public class PromotionManageController implements PromotionManageApi {

    private final static Logger logger = LoggerFactory.getLogger(PromotionManageController.class);

    @Autowired
    JicMarketPromotionService jicMarketPromotionService;

    @Autowired
    PromotionMemberGradeService promotionMemberGradeService;

    /**
     * 分页查询
     * @param servletRequest
     * @param request
     * @return
     */
    @PostMapping("/listByPage")
    @ResponseBody
    @Override
    @ApiOperation(value = "分页查询")
    public RestResult<PageResponse<PromotionResponse>> list(HttpServletRequest servletRequest, @RequestBody PromotionRequest request){
        log.info("分页查询通用活动，入参{}", JSON.toJSONString(request));
        PageResponse<PromotionResponse> entityList = jicMarketPromotionService.listByPage(request);
        log.info("分页查询通用活动，出参{}", JSON.toJSONString(entityList));
        return RestResult.success(entityList);
    }

    /**
     * 创建活动
     * @param servletRequest
     * @param request
     * @return
     */
    @PostMapping("/create")
    @ResponseBody
    @Override
    @ApiOperation(value = "创建活动")
    public RestResult<Long> create(HttpServletRequest servletRequest, @RequestBody PromotionRequest request) {
        logger.info("======create promotion ======{}======",request);
        return RestResult.success(jicMarketPromotionService.addPromotion(request).getId());
    }

    /**
     * 更新活动
     * @param servletRequest
     * @param request
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    @Override
    @ApiOperation(value = "更新活动")
    public RestResult<Long> update(HttpServletRequest servletRequest, @RequestBody PromotionRequest request) {
        // 草稿状态校验活动开始时间校验
        if(PromotionStatusEnum.DRAFT.getCode().equals(request.getStatus())){
            if(1 == new Date().compareTo(request.getStartTime())){
                return RestResult.error("9999", "草稿状态开始活动时间不能小于当前时间");
            }
        }
        return RestResult.success(jicMarketPromotionService.updatePromotion(request));
    }

    /**
     * 获取活动详情
     * @param servletRequest
     * @param request
     * @return
     */
    @Override
    @PostMapping("/detail")
    @ResponseBody
    @ApiOperation(value = "获取活动详情")
    public RestResult<PromotionResponse> detail(HttpServletRequest servletRequest, @RequestBody PromotionRequest request) {
        return RestResult.success(jicMarketPromotionService.detail(request));
    }

    /**
     * 删除活动
     * @param servletRequest
     * @param request
     * @return
     */
    @Override
    @PostMapping("/delete")
    @ResponseBody
    @ApiOperation(value = "获取活动详情")
    public RestResult delete(HttpServletRequest servletRequest, @RequestBody PromotionRequest request) {
        jicMarketPromotionService.delete(request.getId());
        return RestResult.success(null);
    }

    @Override
    @PostMapping("/close")
    @ResponseBody
    public RestResult close(HttpServletRequest servletRequest, @RequestBody PromotionRequest request) {
//        PromotionRequest param = new PromotionRequest();
//        param.setStatus(6);
//        param.setId(request.getId());
//        jicMarketPromotionService.update(param);
        return RestResult.success(null);
    }

    @PostMapping("/memberGrade/add")
    @ResponseBody
    @ApiOperation(value = "新增会员等级")
    public RestResult<Long> addMemberGrade(@RequestBody MemberGradeInfoRequest request) {
        logger.info("======add promotion member grade ======{}======",request);
        return RestResult.success(promotionMemberGradeService.addMemberGrade(request));
    }

    @PostMapping("/memberGrade/update")
    @ResponseBody
    @ApiOperation(value = "更新会员等级")
    public RestResult<Long> updateMemberGrade(@RequestBody MemberGradeInfoRequest request) {
        logger.info("======update promotion member grade ======{}======",request);
        return RestResult.success(promotionMemberGradeService.updateMemberGrade(request));
    }

    @PostMapping("/memberGrade/query")
    @ResponseBody
    @ApiOperation(value = "查询会员等级")
    public RestResult<List<PromotionMemberGradeRequest>> queryMemberGrade(@RequestParam String promotionCode) {
        logger.info("======query promotion member grade ======{}======",promotionCode);
        return RestResult.success(promotionMemberGradeService.queryMemberGrade(promotionCode));
    }

    @PostMapping("/memberGrade/delete")
    @ResponseBody
    @ApiOperation(value = "删除会员等级")
    public RestResult<Long> deleteMemberGrade(@RequestParam String promotionCode) {
        logger.info("======query promotion member grade ======{}======",promotionCode);
        return RestResult.success(promotionMemberGradeService.deleteMemberGrade(promotionCode));
    }

    @PostMapping("/memberGrade/copy")
    @ResponseBody
    @ApiOperation(value = "复制会员等级")
    public RestResult<Long> copyMemberGrade(@RequestParam String oldPromotionCode, @RequestParam String promotionId, @RequestParam String promotionCode) {
        logger.info("======query promotion member grade ======{}======",promotionCode);
        return RestResult.success(promotionMemberGradeService.copyMemberGrade(oldPromotionCode, promotionId, promotionCode));
    }

    /**
     * 复制活动
     * @param servletRequest
     * @param request
     * @return
     */
    @PostMapping("/copy")
    @ResponseBody
    @Override
    @ApiOperation(value = "复制活动")
    public RestResult copy(HttpServletRequest servletRequest, @RequestBody PromotionRequest request) {
        jicMarketPromotionService.copy(request.getId());
        return  RestResult.success(null);
    }

    /**
     * 更新状态（审核，关闭。。）
     * @param servletRequest
     * @param request
     * @return
     */
    @PostMapping("/updateStatus")
    @ResponseBody
    @Override
    @ApiOperation(value = "更新状态（审核，关闭。。）")
    public RestResult updateStatus(HttpServletRequest servletRequest, @RequestBody PromotionRequest request) {
        jicMarketPromotionService.updateStatus(request);
        return  RestResult.success(null);
    }

    /**
     * 提交审核
     * @param request
     * @return
     */
    @PostMapping("/reviewPromotion")
    @ResponseBody
    @Override
    @ApiOperation(value = "提交审核")
    public RestResult reviewPromotion( @RequestBody PromotionRequest request) {

        return jicMarketPromotionService.reviewPromotion(request);

    }


    @ApiOperation(value = "复制")
    @PostMapping("deepCopy")
    public RestResult<Long> deepCopy(@RequestBody PromotionRequest request) {
        return jicMarketPromotionService.deepCopy(request);
    }

    @ApiOperation(value = "保存为模板")
    @PostMapping("saveTemplate")
    public RestResult<Long> saveTemplate(@RequestBody PromotionRequest request) {
        return jicMarketPromotionService.saveTemplate(request);
    }


    @ApiOperation(value = "查询所有待开始进行中的活动,除抽奖优惠券红包")
    @PostMapping("/queryPromotionList")
    public RestResult<PageResult<PromotionResponse>> queryPromotionList(@RequestBody QueryPromotionListRequest request) {
        return jicMarketPromotionService.queryPromotionList(request);
    }

}
